"""
 -*- coding: utf-8-*-
 @File: mgt_data_save.py
 @Author: chen
 @time: 2023/09/15/ 21:02
"""
import datetime
import os.path
import allure

from tools.record_log import logger

# timestamp = int(time.time())
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
cur_path = os.path.dirname(os.path.abspath(__file__))


def save_screenshot(dr):
    picture_path = os.path.join(cur_path, f"../screenshot/screenshot_{now}.PNG")
    picture_path = os.path.abspath(picture_path)
    # print(picture_path)
    dr.save_screenshot(picture_path)
    logger.info("保存截图--->成功")
    allure.attach.file(picture_path, name="pictures", attachment_type=allure.attachment_type.PNG)
    logger.info("上传截图--->成功")


def save_page_source(dr):
    page_source_path = os.path.join(cur_path, f"../page_source/page_source_{now}.HTML")
    page_source_path = os.path.abspath(page_source_path)

    with open(page_source_path, 'w', encoding="utf-8") as f:
        f.write(dr.page_source)
        logger.info("保存网页源码--->成功")
    allure.attach.file(page_source_path, name="page_source", attachment_type=allure.attachment_type.HTML)
    logger.info("上传网页源码--->成功")
